﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Dispatcher.Helpers;

namespace Dispatcher.Views
{
    /// <summary>
    /// Interaction logic for PersonView.xaml
    /// </summary>
    public partial class PersonView
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PersonView"/> class.
        /// </summary>
        public PersonView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the PreviewKeyDown event of the ItemsDataGrid control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
        private void ItemsDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                ItemsDataGrid.CommitEdit();
            }
        }

        /// <summary>
        /// Handles the KeyDown event of the ItemsDataGrid control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
        private void ItemsDataGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (ItemsDataGrid.CurrentCell.Column.DisplayIndex == 5)
            {
                ItemsDataGrid.BeginEdit();
            }
        }

        /// <summary>
        /// Handles the CellEditEnding event of the ItemsDataGrid control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.DataGridCellEditEndingEventArgs"/> instance containing the event data.</param>
        private void ItemsDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        { 
            e.Row.BindingGroup.UpdateSources();
            RefreshDataGridRow(e.Row);
        }

        /// <summary>
        /// Refreshes the data grid rows.
        /// </summary>
        /// <param name="rows">The rows.</param>
        private void RefreshDataGridRows(IEnumerable<DataGridRow> rows)
        {
            ItemsDataGrid.CancelEdit();
            
            foreach (var row in rows)
            {
                RefreshDataGridRow(row);
            }
        }

        /// <summary>
        /// Updates the data grid row.
        /// </summary>
        /// <param name="row">The row.</param>
        private void RefreshDataGridRow(DataGridRow row)
        {
            var tmp = row.Item;
            row.Item = null;
            row.Item = tmp;
        }

        /// <summary>
        /// Called when [save changes executed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Dispatcher.ViewModels.ExtendedEventArgs"/> instance containing the event data.</param>
        public void OnSaveChangesExecuted(object sender, ExtendedEventArgs e)
        {
            var rows = ItemsDataGrid.GetChildren<DataGridRow>();
            RefreshDataGridRows(rows);

            if (e.HasException)
            {
                MessageBox.Show(e.Exception.Message + Environment.NewLine + e.Exception.InnerException.Message);
            }
            else
            {
                MessageBox.Show("Saved.");
            }
        }

        /// <summary>
        /// Called when [refresh].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Dispatcher.ViewModels.ExtendedEventArgs"/> instance containing the event data.</param>
        public void OnRefresh(object sender, EventArgs e)
        {
            var rows = ItemsDataGrid.GetChildren<DataGridRow>();
            RefreshDataGridRows(rows);
        }

        /// <summary>
        /// Called when [undo changes executed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Dispatcher.ViewModels.ExtendedEventArgs"/> instance containing the event data.</param>
        public void OnUndoChangesExecuted(object sender, ExtendedEventArgs e)
        {
            var rows = ItemsDataGrid.GetChildren<DataGridRow>();
            RefreshDataGridRows(rows);
        }

        private void DocumentContent_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && sender is DataGrid)
            {
                DragDrop.DoDragDrop(ItemsDataGrid, ItemsDataGrid.CurrentCell.Item, DragDropEffects.Copy);
            }
        }
    }
}
